<template>
	<view class="container">
		<view class="search-box">
			<view class="input-box" >
				<uni-easyinput 
					prefixIcon="search"
					placeholder="请输入关键词搜索"
					:inputBorder="false" 
					:styles="{color: '#2e2e2e', backgroundColor: '#fff'}" >	
				</uni-easyinput>
				<view class="input-empty" @click="toSearch"></view>
			</view>
		</view>
		<view class="categories">
			<scroll-view scroll-y="true" class="cate" :style="{height: wh + 'px'}">
				<view
				  v-for="(item, index) in categories"
				  :key="index"
				  class="item"
				  :class="{ active: index === activeIndex }"
				  @click="chooseCate(index)">
				  {{item.name}}
				</view>
			</scroll-view>
			<scroll-view scroll-y="true" class="content" :style="{height: wh + 'px'}" @scrolltolower="loadMore" :scroll-top="scrollTop">
				<uni-list :border="false">
					<uni-list-item v-for="item in goods" :key="item.goods.id" class="item-box" :border="false" :to="`/pages/goodsDetail/index?id=${item.goods.id}`">
						<template v-slot:header>
							<image class="pic" :lazy-load="true" :src="item.goods.thumbnail ? item.goods.thumbnail : item.goods.goods_thumbnail" mode="aspectFill"></image>
						</template>
						<template v-slot:body>
							<view class="order-content">
								<view class="good-name">{{item.goods.title}}</view>
								<view class="good-block">
									<view class="good-qune">{{item.back_independent === -1 ? '全网排队':'单店排队'}}</view>
									<view class="good-return">可免单¥{{isLeader ? (item.goods.share_price * item.back_self_ratio / 100 /100 ).toFixed(2) : (item.goods.sale_price * item.back_self_ratio / 100 /100 ).toFixed(2)}}</view>
								</view>
								<view class="good-foot">
									<view class="flex-center">
										<template v-if="isLeader">
											<goods-price :price="item.goods.share_price" size="small"></goods-price>
										</template>
										<template v-else>
											<goods-price :price="item.goods.sale_price" size="small"></goods-price>
										</template>
									</view>
								</view>
							</view>
						</template>
					</uni-list-item>
				</uni-list>
			</scroll-view>
		</view>
	</view>
</template>

<script setup>
	import { ref } from 'vue';
	import { indexCate, indexGoods } from '../../http/apis.js';
	import { useGetters } from '../../store/hooks/useGetters';
	import { onShow } from '@dcloudio/uni-app'
	
	const wh = ref(500);
	const { isLeader } = useGetters(['isLeader']);
	
	onShow(() => {
		const { windowHeight } = uni.getSystemInfoSync();
		wh.value = windowHeight - uni.upx2px(128);
		
		const id = getApp().globalData.category_id;
		const globalCategories = getApp().globalData.categories;
		if (globalCategories.length === 0 && categories.value.length === 0) {
			getCategory(id);
		} else {
			if (!globalCategories[0].goods) {
				globalCategories.forEach((item,i) => {
					item.goods = [];
					item.page = 1;
					item.next_page = 1;
				})
			}
			ininCate(globalCategories, id)
		}
	})
	
	// 左侧导航
	const scrollTop = ref(0);
	const activeIndex = ref(0);
	const chooseCate = (index) => {
		if (index == activeIndex.value) return;
		activeIndex.value = index;
		scrollTop.value = scrollTop.value ? 0 : 1;
		goods.value = [];
		if (categories.value[index].goods.length > 0) {
			goods.value = categories.value[index].goods;
		} else {
			getGoods(index)
		}
		getApp().globalData.category_id = categories.value[index].id;
	}
	
	// 获取分类对应的商品
	const goods = ref([]);
	const currentPage = ref(1);
	const nextPage = ref(1);
	const isLoading = ref(false);
	// i 分类索引
	const getGoods = (i) => {
		if (isLoading.value) return;
		isLoading.value = true;
		const currentNav = categories.value.length > 0 ? categories.value[i] : {id: 0, page: 1, next_page: 1, goods: []};
		let params = {
			category_id: currentNav.id,
			page: currentNav.page,
		}
		indexGoods(params).then(res => {
			if (currentNav.page > 1) {
				currentNav.goods = currentNav.goods.concat(res.data)
			} else {
				currentNav.goods = res.data || [];
			}
			currentNav.next_page = res.next_page;
			categories.value[i] = currentNav;
			goods.value = currentNav.goods;
			isLoading.value = false;
		}).catch(res => {
			isLoading.value = false;
		})
	}
	const loadMore = () => {
		const currentNav = categories.value[activeIndex.value];
		if (currentNav.page < currentNav.next_page && !isLoading.value) {
			categories.value[activeIndex.value].page += 1;
			getGoods(activeIndex.value)
		}
	}
	
	const categories = ref([]);
	const getCategory = (id) => {
		indexCate().then(res => {
			res.sort((a, b) => a.sort - b.sort)
			getApp().globalData.categories = res;
			res.forEach((item,i) => {
				item.goods = [];
				item.page = 1;
				item.next_page = 1;
			})
			ininCate(res, id)
		})
	}
	const ininCate = (cates, id) => {
		categories.value = cates;
		let aIndex = 0;
		cates.forEach((item,i) => {
			if (id === item.id) {
				aIndex = i;
				if (item.goods.length === 0) {
					getGoods(aIndex);
					scrollTop.value = scrollTop.value ? 0 : 1;
				}
			}
		})
		if (aIndex === 0 && categories.value[0].goods.length === 0) getGoods(aIndex);
		activeIndex.value = aIndex;
		getApp().globalData.category_id = categories.value[aIndex].id;
	}
	const toSearch = () => {
		uni.navigateTo({
			url: `/pages/search/index`
		})
	}
</script>

<style lang="scss" scoped>
	.container {
		width: 100%;
		height: 100vh;
		position: relative;
		.categories {
			display: flex;
			flex: 1;
			min-height: 400rpx;
			.cate {
				overflow: hidden;
				width: 200rpx;
				flex: none;
				background-color: #F8FAFD;
				.item {
					display: flex;
					justify-content: center;
					align-items: center;
					height: 88rpx;
					font-size: 28rpx;
					color: #6E7073;
					position: relative;
					&.active {
						background-color: #fff;
						color: #2E2E2E;
					}
				}
			}
			.content {
				width: 100%;
				padding: 0 0rpx 0rpx 20rpx;
				box-sizing: border-box;
				.empty {
					width: 100%;
					height: 1rpx;
				}
				::v-deep .uni-list-item__container {
					padding: 0;
				}
				.item-box {
					width: 100%;
					height: 152rpx;
					margin-bottom: 40rpx;
					display: flex;
					padding: 0 20rpx 0 0;
					box-sizing: border-box;
					.pic {
						margin-right: 16rpx;
						width: 152rpx;
						height: 152rpx;
						border-radius: 8rpx;
					}
					.order-content {
						flex: 1;
						overflow: auto;
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						.good-name {
							height: 56rpx;
							color: #000;
							font-size: 24rpx;
							line-height: 28rpx;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-line-clamp: 2;
							-webkit-box-orient: vertical 
						}
						.good-block {
							display: flex;
							align-items: center;
						}
						.good-return {
							padding: 0 8rpx;
							box-sizing: border-box;
							background: rgba(252, 73, 81, 0.1);
							font-size: 24rpx;
							font-weight: normal;
							line-height: 32rpx;
							height: 32rpx;
							color: #FC4951;
							border-radius: 4rpx;
							margin-left: 10rpx;
						}
						.good-qune {
							padding: 0 8rpx;
							box-sizing: border-box;
							font-size: 24rpx;
							line-height: 32rpx;
							height: 32rpx;
							color: #8D6412;
							font-weight: normal;
							background: rgba(219, 149, 11, 0.1);
						}
						.good-foot {
							 display: flex;
							 justify-content: space-between;
							 align-items: center;
							 .good-number {
								 color: #2E2E2E;
								 font-size: 28rpx;
								 line-height: 32rpx;
							 }
						 }
					}
				}
			}
		}
	}
	.search-box {
		width: 100%;
		height: 128rpx;
		background-color: #fff;
		padding: 24rpx 24rpx 34rpx 24rpx;
		box-sizing: border-box;
		position: sticky;
		top: -2rpx;
		z-index: 10;
		.input-box {
			width: 100%;
			height: 70rpx;
			border-radius: 35rpx;
			display: flex;
			align-items: center;
			position: relative;
			.input-empty {
				width: 100%;
				height: 100%;
				position: absolute;
				top: 0;
				left: 0;
				z-index: 2;
			}
		}
		::v-deep .uni-easyinput__content {
			background: #F8FAFD !important;
			border-radius: 35rpx;
		}
		::v-deep .uni-easyinput__content-input {
			font-size: 28rpx;
			color: #2e2e2e;
		}
	}
</style>